//
//  MKUIElementViewTextArea.m
//  MKUI
//
//  Created by 苏尚进 on 2023/7/13.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUIElementViewTextArea.h"
#import "UIColor+MKUI.h"
#import "MKUIPreCompile.h"

@interface __MKUIElementViewTextAreaInputAccessoryView : UIView
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end
@implementation __MKUIElementViewTextAreaInputAccessoryView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.doneButton = [[UIButton alloc] init];
        [self.doneButton setTitle:@"Done" forState:(UIControlStateNormal)];
        self.doneButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.doneButton setTitleColor:UIColor.systemBlueColor forState:(UIControlStateNormal)];
        [self addSubview:self.doneButton];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.doneButton] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_R) contentInsets:UIEdgeInsetsMake(0, 20, 0, 20) interitemSpacing:0];
        self.backgroundColor = [UIColor mk_colorWithLight:UIColor.lightGrayColor dark:UIColor.darkGrayColor];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    UIEdgeInsets insets = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        insets = self.safeAreaInsets;
    }
    CGRect f1 = UIEdgeInsetsInsetRect(bounds, insets);
    self.flowlayout.bounds = f1;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 44);
}
- (CGSize)intrinsicContentSize{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 44);
}
@end

@interface MKUIElementViewTextArea()
@property(nonatomic,strong) UITextView *textView;
@property(nonatomic,strong) __MKUIElementViewTextAreaInputAccessoryView *textViewAccessoryView;
@end

@implementation MKUIElementViewTextArea
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.textView = [[UITextView alloc] init];
        self.textView.backgroundColor = UIColor.clearColor;
        self.textView.font = [UIFont systemFontOfSize:16];
        self.textView.textAlignment = NSTextAlignmentRight;
        self.textView.delegate = self;
        [self.contentView addSubview:self.textView];
        self.textViewAccessoryView = [[__MKUIElementViewTextAreaInputAccessoryView alloc] init];
        [self.textViewAccessoryView sizeToFit];
        [self.textViewAccessoryView.doneButton addTarget:self action:@selector(__onDoneButtonTap:) forControlEvents:(UIControlEventTouchUpInside)];
        self.textView.inputAccessoryView = self.textViewAccessoryView;
        
        @MK_WEAKIFY(self);
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.textView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            @MK_NORMALIZE(self);
            CGSize s = [self.textView sizeThatFits:size];
            s.width = size.width;
            s.height = MAX(s.height,36);//一行
            s.height = MIN(s.height,55+40);//四行
            return s;
        }];
        self.textViewLayoutConstraint = w1;
        //
        [self.segLayoutConstraint addItem:w1];
    }
    return self;
}
- (void)setText:(NSString *)text{
    if(_text==text||[_text isEqualToString:text])return;
    _text = text;
    self.textView.text = text;
}
- (void)__onTextValueChange:(id)sender{
    self.text = self.textView.text;
    if(self.whenChange){
        self.whenChange(self, self.textView.text);
    }
}
- (void)__onDoneButtonTap:(id)sender{
    [self.textView endEditing:YES];
    if(self.whenDone){
        self.whenDone(self);
    }
}
- (void)setDoneButtonText:(NSString *)doneButtonText{
    [self.textViewAccessoryView.doneButton setTitle:doneButtonText forState:(UIControlStateNormal)];
}
- (NSString *)doneButtonText{
    return [self.textViewAccessoryView.doneButton titleForState:(UIControlStateNormal)];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGFloat height = [self titleAndSubTitleHeightThatFits:size];
    CGSize s1 = [self sizeThatFits:size contentSizeBlock:^CGSize(CGSize contentSize) {
        return [self.segLayoutConstraint sizeThatFits:contentSize resizeItems:YES];
    }];
    s1.height = MAX(height,s1.height);
    return s1;
}
#pragma mark - delegate:UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    [self __onTextValueChange:textView];
}
@end

@implementation MKUIElementListViewCellTextArea
+ (Class)elementViewClass{
    return MKUIElementViewTextArea.class;
}
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.enableSelection = NO;
    }
    return self;
}
@end
